﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace WebCrawler
{
    public class WebCrawlerController
    {
        private IBasicGraph sitemap;
        private Vertex startVertex;
        private int foundLinks;
        private Status status = Status.GetInstance();

        public WebCrawlerController(string url, int limit)
        {
            foundLinks = 0;
            sitemap = new BasicGraph();
            startVertex = new Vertex(url);
            String doc = GetStream(url);
            startVertex.Doc.Document = doc;
            sitemap.AddVertex(startVertex);
            BuildSiteMap(startVertex, limit, url);
        }

        // bygger en vertex
        public WebCrawlerController()
        {
        }

        public IBasicGraph Sitemap
        {
            get { return sitemap; }
            set { sitemap = value; }
        }

        public Vertex StartVertex
        {
            get { return startVertex; }
            set { startVertex = value; }
        }

        public int FoundLinks
        {
            get { return foundLinks; }
            set { foundLinks = value; }
        }

        

        private void BuildSiteMap(Vertex v, int limit, String url)
        {
            if (limit > 0)
            {
                foundLinks++;

                status.CrawlerStatus = "Fundet " + foundLinks + " sider";

//                IHttp client = new HttpTcpClient();
//                client.Open(v.Url, 80);
//                client.Send(Util.GetHostHeader(v.Url));

                v.Doc = new HtmlDocRegEx(v.Url, GetStream(url));

//                client.Close();

                sitemap.AddVertex(v);

                IList<string> links = new List<string>();
                links = (IList<string>) v.Doc.GetAllLinks();
                foreach (string link in links)
                {
                    sitemap.AddEdge(v, new Vertex(link));
                }

                limit--;

                foreach (Edge edge in v.Edges)
                {
                    BuildSiteMap(edge.To, limit, url);
                }
            }
        }

        //Request Data Using the WebRequest Class @ http://msdn.microsoft.com/en-us/library/456dfw4f.aspx
        public static String GetStream(String url)
        {
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();

            StreamReader r = new StreamReader(response.GetResponseStream());
            String html = "";

            for (String line = r.ReadLine(); line != null; line = r.ReadLine())
            {
                html += line;
            }

            return html;
        }

        //Finding Links in Simple HTML @ http://oreilly.com/windows/archive/csharp-regular-expressions.html
        public static String GetInnerStream(String url)
        {
            //string html = getStream(url);

            String t18 = @"
            <html>
            <a href=""first.htm"">first tag text</a>
            <a href=""next.htm"">next tag text</a>
            </html>
            ";

            String p18 = @"<A[^>]*?HREF\s*=\s*[""']?"
                         + @"([^'"" >]+?)[ '""]?>";
            MatchCollection mc18 = Regex.Matches(t18, p18);

           return mc18.ToString();

        }
    }


}
